package com.ck.controller.teacher;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.ck.domain.Banji;
import com.ck.domain.Exam;
import com.ck.domain.Subject;
import com.ck.service.ExamService;
import com.ck.service.QuestionService;
import com.ck.service.TeacherService;

public class AddNewExamController extends SimpleFormController {
	
	private ExamService examService;
	private QuestionService questionService;
	private TeacherService teacherService;
	
	//声明公共变量，供回显表单时使用。
	private List<Subject> subjectList = null;
	private List<Banji> banjiList = null;

	public void setExamService(ExamService examService) {
		this.examService = examService;
	}
	
	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		Map model = new HashMap();
		subjectList = questionService.gainSubjectList();
		banjiList = teacherService.gainBanjiList();
	
		model.put("subjectList", subjectList);
		model.put("banjiList", banjiList);
		return model;
	}
	
	protected ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {
		
		Exam exam = (Exam) command;
		Map model = errors.getModel();
		
		Integer teacherID = (Integer) request.getSession().getAttribute("teacherID");
		
		//后台校验
		boolean flag = true;
		String title = exam.getTitle().trim();
		Integer subjectID = Integer.parseInt(request.getParameter("subject"));
		String remark = exam.getRemark().trim();
		
		String start_time_str = (String) request.getParameter("start_time_str");
		String end_time_str = (String) request.getParameter("end_time_str");
		String[] banji_array = request.getParameterValues("banji");
		
		if(title == null || "".equals(title)) {
			model.put("error_title", "请输入标题");
			flag = false;
		}
		if(title.length() > 60) {
			model.put("error_title", "标题长度：1-60个字符");
			flag = false;
		}
		
		if( subjectID == 0){
			model.put("error_subjectID", "请选择科目");
			flag = false;
		}
		
		DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date start_time = null;
		Date end_time = null;
		
		if( start_time_str == null || "".equals(start_time_str) ){
			model.put("error_start_time", "请输入开始时间");
			flag = false;
		}else{
			try {
				start_time = f.parse(start_time_str);
			} catch(Exception e){
				model.put("error_start_time", "开始时间格式不正确");
				flag = false;
			}
		}
		
		if( end_time_str == null || "".equals(end_time_str) ){
			model.put("error_end_time", "请输入结束时间");
			flag = false;
		}else{
			try {
				end_time = f.parse(end_time_str);
			} catch(Exception e){
				model.put("error_end_time", "结束时间格式不正确");
				flag = false;
			}
		}
		
		try {
			if( (start_time.compareTo(end_time)) >= 0 ){
				model.put("error_start_time", "开始时间应小于结束时间");
				flag = false;
			}
		} catch(Exception e){
			flag = false;
		}
		
		if( banji_array == null || banji_array.length == 0 ){
			model.put("error_banji", "请至少选择一个考试班级");
			flag = false;
		}
		
		if( remark == null || "".equals(remark) ){
			exam.setRemark(null);
        }else{
        	if(remark.length() > 60) {
    			model.put("error_remark", "备注长度：0-60个字符");
    			flag = false;
    		}
        }
		
		//校验不通过
        if(!flag){
        	model.put("title", title);
        	model.put("subjectID", subjectID);
        	model.put("start_time_str", start_time_str);
        	model.put("end_time_str", end_time_str);
        	model.put("banji_array", banji_array);
        	model.put("remark", remark);
        	model.put("subjectList", subjectList);
    		model.put("banjiList", banjiList);
			return new ModelAndView(getFormView(), model);
		}
		
		exam.setTeacher(teacherID);
		exam.setStart_time(start_time);
		exam.setEnd_time(end_time);
		
		boolean isAddExamSuccess = examService.addNewExam(exam, banji_array, model);
		
		if (isAddExamSuccess) {
            return new ModelAndView(getSuccessView(),model);
        } else {
        	model.put("title", title);
        	model.put("subjectID", subjectID);
        	model.put("start_time_str", start_time_str);
        	model.put("end_time_str", end_time_str);
        	model.put("banji_array", banji_array);
        	model.put("remark", remark);
        	model.put("subjectList", subjectList);
    		model.put("banjiList", banjiList);
            return new ModelAndView(getFormView(), model);
        }
	}
}
